package com.mj.mafeed.activity;

import com.mj.mafeed.R;
import com.mj.mafeed.service.MemberService;
import com.mj.mafeed.util.Md5Util;
import com.mj.mafeed.util.NetUtils;
import com.mj.mafeed.util.SPUtils;
import com.mj.mafeed.widget.LoadingDialog;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 密码
 * @author zhaominglei
 * @date 2014-12-19
 * 
 */
public class PasswordActivity extends Activity implements OnClickListener{
	@SuppressWarnings("unused")
	private static final String TAG = PasswordActivity.class.getSimpleName();
	
	private ImageView goHome;
	private TextView accountView; //账户
	private EditText pPasswordEdt;
	private EditText nPasswordEdt; 
	private EditText cfnPasswordEdt; 
	private Button saveBtn;
	private Button cancelBtn;
	private LoadingDialog loadingDialog; //加载框
	private String referer; //来自那个链接
	private String username; //用户名
	private String sign; //签名
	private String pPassword; //当前密码
	private String nPassword; //新密码
	private String cfnPassword; //确认新密码
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_password);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView) findViewById(R.id.password_gohome);
		accountView = (TextView) findViewById(R.id.password_account);
		pPasswordEdt = (EditText) findViewById(R.id.password_pPassword_edt);
		nPasswordEdt = (EditText) findViewById(R.id.password_nPassword_edt);
		cfnPasswordEdt = (EditText) findViewById(R.id.password_cfnPassword_edt);
		saveBtn = (Button) findViewById(R.id.password_save);
		cancelBtn = (Button) findViewById(R.id.password_cancel);
		
		goHome.setOnClickListener(this);
		saveBtn.setOnClickListener(this);
		cancelBtn.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		
		referer = getIntent().getStringExtra("referer");
		username = (String)SPUtils.get(getApplicationContext(), "username", "");
		sign = (String)SPUtils.get(getApplicationContext(), "sign", "");
		
		accountView.setText(username);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.password_gohome:
			intent = new Intent(getApplicationContext(), SettingActivity.class);
			intent.putExtra("referer", "PasswordActivity");
			startActivity(intent);
			PasswordActivity.this.finish();
			break;
			
		case R.id.password_save:
			save(v);
			break;
			
		case R.id.password_cancel:
			intent = new Intent(getApplicationContext(), SettingActivity.class);
			intent.putExtra("referer", "PasswordActivity");
			startActivity(intent);
			PasswordActivity.this.finish();
			break;
			
		default:
			break;
		}
	}

	/**
	 * 保存
	 */
	private void save(View v) {
		pPassword = pPasswordEdt.getText().toString();
		nPassword = nPasswordEdt.getText().toString();
		cfnPassword = cfnPasswordEdt.getText().toString();
		
		if (pPassword == null || pPassword.equals("")) {
			Toast.makeText(getApplicationContext(), R.string.password_check_error1, Toast.LENGTH_SHORT).show();
			return;
		}
		if (nPassword == null || nPassword.equals("")) {
			Toast.makeText(getApplicationContext(), R.string.password_check_error2, Toast.LENGTH_SHORT).show();
			return;
		}
		if (cfnPassword == null || cfnPassword.equals("")) {
			Toast.makeText(getApplicationContext(), R.string.password_check_error3, Toast.LENGTH_SHORT).show();
			return;
		}
		if (!nPassword.equals(cfnPassword)) {
			Toast.makeText(getApplicationContext(), R.string.password_check_error4, Toast.LENGTH_SHORT).show();
			return;
		}
		StringBuilder sb = new StringBuilder();
		sb.append("username=").append(username).append("&password=").append(Md5Util.encrypt(pPassword));
		if (!sign.equals(Md5Util.encrypt(sb.toString()))) {
			Toast.makeText(getApplicationContext(), R.string.password_check_error5, Toast.LENGTH_SHORT).show();
			return;
		}
		if (nPassword.equals(pPassword)) {
			Toast.makeText(getApplicationContext(), R.string.save_success, Toast.LENGTH_SHORT).show();
			PasswordActivity.this.finish();
		} else {
			if (NetUtils.isConnected(getApplicationContext())) {
				loadingDialog.show();
				new PasswordSaveAsyncTask().execute("");
			} else {
				Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
			}
		}
		
		
	}

	@Override
	public void onBackPressed() {
		intent = new Intent(getApplicationContext(), SettingActivity.class);
		intent.putExtra("referer", "PasswordActivity");
		startActivity(intent);
		PasswordActivity.this.finish();
	}

	/**
	 * 修改个人资料
	 */
	public class PasswordSaveAsyncTask extends AsyncTask<String, String, Boolean> {
		@Override
		protected Boolean doInBackground(String... params) {
			return new MemberService().updatePassword(referer, pPassword, cfnPassword, username, sign);
		}
		@Override
		protected void onPostExecute(Boolean result) {
			loadingDialog.dismiss();
			if (result) {
				SPUtils.clear(getApplicationContext());
				Intent intent = new Intent(getApplicationContext(), MainActivity.class);
				intent.putExtra("referer", "PasswordActivity");
				startActivity(intent);
				PasswordActivity.this.finish();
				Toast.makeText(getApplicationContext(), R.string.save_success, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getApplicationContext(), R.string.save_error, Toast.LENGTH_SHORT).show();
			}
		}
	}
}
